package com.nickstumpos.android.GmailWidgetPlus.Configure;

import java.util.HashMap;
import java.util.Map;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.nickstumpos.android.GmailWidgetPlus.R;
import com.nickstumpos.android.GmailWidgetPlus.Configure.OptionsList.OptionFactory;
import com.nickstumpos.android.GmailWidgetPlus.Configure.OptionsList.OptionsList;
import com.nickstumpos.android.GmailWidgetPlus.Configure.OptionsList.options.ConfigurableOptions;
import com.nickstumpos.android.GmailWidgetPlus.Constants.GMWP_Constants;
import com.nickstumpos.android.GmailWidgetPlus.labels.LabelBlackListManager;
import com.nickstumpos.android.GmailWidgetPlus.widget.sizes.WidgetSize;

public abstract class ConfigureGmailWidgetPlus extends Activity {

	private int									widgetId		= AppWidgetManager.INVALID_APPWIDGET_ID;
	private Map<Integer, ConfigurableOptions>	selection;
	private ListView							lv				= null;
	private ArrayAdapter<String>				aa				= null;
	private View								okButton		= null;
	private View								cancelButton	= null;

	public ConfigureGmailWidgetPlus() {

		super();
		this.selection = new HashMap<Integer, ConfigurableOptions>();
	}

	private void enableChooseOption(final OptionsList c) {

		ConfigureGmailWidgetPlus.this.addContentView(c,
				new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
						LayoutParams.FILL_PARENT));
		c.setVisibility(View.VISIBLE);
		c.requestFocus();
	}

	@Override
	public void finish() {

		if (this.selection != null) {
			this.selection.clear();
			this.selection = null;
		}
		this.lv = null;
		this.aa = null;
		this.okButton = null;
		this.cancelButton = null;
		super.finish();
	}

	private String getGWIDAction() {

		String action = null;
		final WidgetSize size = ConfigureGmailWidgetPlus.this.getSize();
		if (size != null) {
			action = size.getAction();
		}

		return action;
	}

	protected abstract WidgetSize getSize();

	private void initViews() {

		this.lv = (ListView) this.findViewById(R.id.MainConfigList);
		this.aa = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1);
		final LayoutInflater lf = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View v = lf.inflate(R.layout.choose_option_choice, null);
		if ((v != null) && (this.lv != null)) {
			this.lv.addFooterView(v, null, false);
			this.okButton = v.findViewById(R.id.OK_OP);
			this.cancelButton = v.findViewById(R.id.CANCEL_OP);
		}
	}

	@Override
	public void onCreate(final Bundle saved) {

		super.onCreate(saved);
		this.setResult(Activity.RESULT_CANCELED);
		final Intent intent = this.getIntent();
		final Bundle extras = intent.getExtras();
		if (extras != null) {
			this.widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		if (this.widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			this.finish();
		}
		this.setContentView(R.layout.config_layout);
		this.setDefaults();
		this.initViews();
		this.setupListView();
		this.setupOkButton();
		this.setupCancelButton();
	}

	private void setDefaultAccount() {

		final SharedPreferences prefs = this.getSharedPreferences(GMWP_Constants.GMW_PREFS_REF,
				0);
		final String name = prefs.getString(GMWP_Constants.PREF_ACCOUNT_REF + this.widgetId,
				null);
		if (name == null) {
			final Account[] accounts = AccountManager.get(this)
					.getAccountsByType(GMWP_Constants.GOOGLE_ACCOUNT_TYPE);
			if ((accounts != null) && (accounts[0] != null)) {
				final SharedPreferences.Editor ed = prefs.edit();
				ed.putString(GMWP_Constants.PREF_ACCOUNT_REF + this.widgetId,
						accounts[0].name);
				ed.commit();
			}
		}
	}

	private void setDefaultBlacklist() {

		LabelBlackListManager.initBlacklist(this, this.widgetId);
	}

	private void setDefaults() {

		this.setDefaultAccount();
		this.setDefaultBlacklist();
	}

	private void setupCancelButton() {

		this.cancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {

				ConfigureGmailWidgetPlus.this.finish();
			}
		});
	}

	private void setupListView() {

		int pos = 0;
		for (final ConfigurableOptions type : ConfigurableOptions.values()) {
			this.aa.add(this.getString(type.getDisplayValue()));
			this.selection.put(pos, type);
			pos++;
		}
		this.lv.setAdapter(this.aa);
		this.lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(final AdapterView<?> parentView,
					final View childView,
					final int position,
					final long id) {

				ConfigureGmailWidgetPlus.this.enableChooseOption(OptionFactory.createOptionsList(ConfigureGmailWidgetPlus.this,
						ConfigureGmailWidgetPlus.this.selection.get(position),
						ConfigureGmailWidgetPlus.this.widgetId,
						ConfigureGmailWidgetPlus.this.getSize()));
			}
		});
	}

	private void setupOkButton() {

		this.okButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {

				final SharedPreferences.Editor prefs = ConfigureGmailWidgetPlus.this.getSharedPreferences(GMWP_Constants.GMW_PREFS_REF,
						0)
						.edit();
				prefs.putBoolean(GMWP_Constants.CAN_RUN + String.valueOf(ConfigureGmailWidgetPlus.this.widgetId),
						true);
				prefs.commit();
				ConfigureGmailWidgetPlus.this.sendBroadcast(new Intent() {

					{
						this.setAction(ConfigureGmailWidgetPlus.this.getGWIDAction());
						this.putExtra(GMWP_Constants.EXTRA_WIDGET_ACTION,
								GMWP_Constants.WIDGET_NEW_WIDGET);
						this.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
								ConfigureGmailWidgetPlus.this.widgetId);
					}
				});
				final Intent resultValue = new Intent();
				resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
						ConfigureGmailWidgetPlus.this.widgetId);
				ConfigureGmailWidgetPlus.this.setResult(Activity.RESULT_OK,
						resultValue);
				ConfigureGmailWidgetPlus.this.finish();
			}
		});
	}
}
